﻿using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Logging
{
    public sealed class WindowsEventLogger : LoggerBase, ILogger
    {
        public void Log(LogEntry Entry)
        {
            if (!IsLoggingEnabled()) { return; }

            EventLogEntryType ty = EventLogEntryType.Error;

            if (!EventLog.SourceExists(Entry.Source))
            {
                EventLog.CreateEventSource(Entry.Source, "Application");
            }

            switch (Entry.Type)
            {
                case EntryType.Error: ty = EventLogEntryType.Error; break;
                case EntryType.FailureAudit: ty = EventLogEntryType.FailureAudit; break;
                case EntryType.Information: ty = EventLogEntryType.Information; break;
                case EntryType.SuccessAudit: ty = EventLogEntryType.SuccessAudit; break;
                case EntryType.Warning: ty = EventLogEntryType.Warning; break;
            }

            EventLog.WriteEntry(Entry.Source, string.Format("{0}\n{1}", Entry.Title, Entry.Message), ty, Entry.EventID, 0);
        }

        public void Log(string Source, string Title, string Message, EntryType Type)
        {
            Log(Source, Title, Message, Type, DateTime.Now, 0);
        }

        public void Log(string Source, string Title, string Message, EntryType Type, DateTime TimeStamp)
        {
            Log(Source, Title, Message, Type, TimeStamp, 0);
        }

        public void Log(string Source, string Title, string Message, EntryType Type, int EventID)
        {
            Log(Source, Title, Message, Type, DateTime.Now, EventID);
        }

        public void Log(string Source, string Title, string Message, EntryType Type, DateTime TimeStamp, int EventID)
        {
            LogEntry entry = new LogEntry();
            entry.Source = Source;
            entry.Title = Title;
            entry.Message = Message;
            entry.Type = Type;
            entry.TimeStamp = TimeStamp;
            entry.EventID = EventID;
            Log(entry);
        }

    }
}
